import com.sun.org.glassfish.gmbal.AMXMBeanInterface;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 26727
 * Date: 2024-02-20
 * Time: 21:40
 */


public class Sort {
    //直接选择排序
    //时间复杂度：O(N^2) [等差数列]
    //空间复杂度：O(1)
    //稳定性：不稳定
    public static void selectSort(int[] array) {
        for(int i = 0; i < array.length; i++) {
            for(int j = i+1; j < array.length;j++) {
                while(array[i] > array[j]) {
                    int tmp = array[i];
                    array[i] = array[j];
                    array[j] = tmp;
                }
            }
        }
    }

    public static void selectSort1(int[] array) {
        for(int i = 0; i < array.length; i++) {
            int minIndex = i;
            for(int j = i+1; j < array.length;j++) {
                if(array[j] < array[minIndex]) {
                    minIndex = j;
                }
            }
            swap(array,i,minIndex);
        }
    }

    private static void swap(int[] array,int i,int j) {
        int tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
    }

    public static void selectSort2(int[] array) {
        int left = 0;
        int right = array.length-1;
            while (left < right) {
                int minIndex = left;
                int maxIndex = left;

                for(int i = left+1;i <= right; i++) {
                    if(array[i] < array[minIndex]) {
                        minIndex = i;
                    }
                    if(array[i] > array[maxIndex]) {
                        maxIndex = i;
                    }
                }
                //这里有可能把最大值换到minIndex的位置(最大值正好在left地方)
                swap(array,minIndex,left);
                if(maxIndex == left) {
                    maxIndex = minIndex;
                }
                swap(array,maxIndex,right);
                left++;
                right--;
            }
        }

        //堆排序
        //时间复杂度：O(N*logN)
        //空间复杂度：O(1)
        //稳定性：不稳定
        public static void heapSort(int[] array) {
            createBigHeap(array);
            int end = array.length-1;
            while(end > 0) {
                swap(array,0,end);
                shiftDown(array,0,end);
                end--;
            }
        }

        private static void createBigHeap(int[] array) {
            for(int parent = (array.length-1-1)/2; parent >= 0; parent--) {
                shiftDown(array,parent,array.length);
            }
        }

        private static void shiftDown(int[] array,int parent, int len) {
            int child = 2*parent+1;
            while(child < len) {
                if(child+1 < len && array[child] < array[child+1]) {
                    child++;
                }
                if(array[child] > array[parent]) {
                    swap(array,child,parent);
                    parent = child;
                    child = 2*parent+1;
                }else {
                    break;
                }
            }
        }
}
